package com.winit.attachment.manager;

import com.winit.attachment.model.Attachment;
import com.winit.attachment.repository.AttachmentRepository;
import com.winit.common.util.TableIdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@Component
public class AttachmentManager implements InitializingBean {
    private static Logger log = LoggerFactory.getLogger(AttachmentManager.class);

    private String ATTACHMENT_PATH_BRD;
    private String ATTACHMENT_PATH_PRD;
    private String ATTACHMENT_PATH_REQ;
    private String ATTACHMENT_PATH_COMMON;
    @Value("${attachment.base.path}")
    private String ATTACHMENT_BASE_PATH;


    @Autowired
    private AttachmentRepository attachmentRepository;

    /**
     * 初始化attachment路径
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
//        if (ATTACHMENT_PATH_RELATIVE) {
        if (StringUtils.isEmpty(ATTACHMENT_BASE_PATH)) {
            ATTACHMENT_BASE_PATH = System.getProperty("user.dir");
        }
        ATTACHMENT_PATH_BRD = ATTACHMENT_BASE_PATH + File.separator + "brd";
        ATTACHMENT_PATH_PRD = ATTACHMENT_BASE_PATH + File.separator + "prd";
        ATTACHMENT_PATH_REQ = ATTACHMENT_BASE_PATH + File.separator + "req";
        ATTACHMENT_PATH_COMMON = ATTACHMENT_BASE_PATH + File.separator + "common";
//        }
        log.info("attachment path::ATTACHMENT_PATH_BRD:" + ATTACHMENT_PATH_BRD + ";ATTACHMENT_PATH_PRD:" + ATTACHMENT_PATH_PRD + ";ATTACHMENT_PATH_COMMON:" + ATTACHMENT_PATH_COMMON);
    }

    private String getStoragePath(String resourceType) {
        String path;
        switch (resourceType) {
            case "BRD":
                path = ATTACHMENT_PATH_BRD;
                break;
            case "PRD":
                path = ATTACHMENT_PATH_PRD;
                break;
            case "REQ":
                path = ATTACHMENT_PATH_REQ;
                break;
            default:
                path = ATTACHMENT_PATH_COMMON;
        }
        return path;
    }

    public Attachment uploadFile(MultipartFile multipartFile, String resourceType) throws Exception {
        String fileName = multipartFile.getOriginalFilename();
        String attachmentId = TableIdUtil.createAttachmentId();
        String basePath = this.getStoragePath(resourceType);
        File directory = new File(basePath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File targetFile = new File(directory, attachmentId);
        if (!targetFile.exists()) {
            try {
                targetFile.createNewFile();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw e;
            }
        }
        try {
            multipartFile.transferTo(targetFile);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        Attachment attachment = new Attachment();
        attachment.setAttachmentId(attachmentId);
        attachment.setAttachmentName(fileName);
        attachment.setAttachmentIsDel("1");
        attachment.setAttachmentPath(basePath);
        this.attachmentRepository.save(attachment);

        return attachment;
    }

    /**
     * 该方法只返回未删除的attachment
     *
     * @param attachmentId
     * @return
     */
    public Attachment getAttachmentById(String attachmentId) {
        return this.attachmentRepository.findByAttachmentIdAndAttachmentIsDel(attachmentId, "1");
    }
}
